


#include <functional>
#include <iostream>
#include <memory>
#include <vector>

#include "Coroutine.h"

class Scheduler {
  public:
    void scheduler(std::shared_ptr<zepco::Zephyr> task) {
        m_tasks.push_back(task);
    }

    void run() {
        std::cout << " number " << m_tasks.size() << std::endl;

        std::shared_ptr<zepco::Zephyr> task;
        auto it = m_tasks.begin();
        while(it != m_tasks.end()) {
            // 迭代器本身也是指针
            task = *it;
            // 由主协程切换到子协程，子协程函数运行完毕后自动切换到主协程
            task->resume();
            it++;
        }
        m_tasks.clear();
    }
  private:
    std::vector<std::shared_ptr<zepco::Zephyr>> m_tasks;
};

void test_zephyr(int i) {
    std::cout << "hello world " << i << std::endl;
}

int main() {

    zepco::Zephyr::GetThis();

    Scheduler sc;

    for (auto i = 0; i < 20; i++) {
        std::shared_ptr<zepco::Zephyr> zephyr = std::make_shared<zepco::Zephyr>(std::bind(test_zephyr, i), 0, false);
        sc.scheduler(zephyr);
    }

    sc.run();

    return 0;
}